/**
 * Project Name:smcs-mass-dns-ipcheck
 * File Name:IpAllocateStorageScanner.java
 * Package Name:com.surfilter.massdata.ipcheck.mr.helper
 * Date:2015-3-28下午05:11:32
 * Copyright (c) 2015, Shenzhen Surfilter Network Technology Co.,Ltd All Rights Reserved.
 * 
*/

package com.surfilter.massdata.spark.util.ipcheck;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.surfilter.massdata.spark.bean.ipcheck.ErrIp;
import com.surfilter.massdata.spark.bean.ipcheck.IpBean;
import com.surfilter.massdata.spark.bean.ipcheck.ObjectInfo;
/** 
* @author  hutao
* @date 创建时间：2017年5月27日 下午3:14:52 
* @Description：扫描分配库，参照来源库
* @version 1.0 
* 
**/
public class IpAllocateStorageScanner {

	public static ObjectInfo scanAllocateStorage(List<IpBean> reduceSourceList, List<IpBean> reduceAllocateList, Map<Long, String> bsdwMap) throws IOException, InterruptedException{
		ObjectInfo objectInfo=new ObjectInfo();
		objectInfo.setResult(true);
		List<IpBean> allocateList = reduceAllocateList;
		for(IpBean allocate : allocateList){
//			log.info(key.toString() + "|" + allocate.toString());
			if(null==allocate.getIsPass() ||!new Boolean(false).equals(allocate.getIsPass())){
				boolean flag = false;//是否有对应的来源信息
				Long bbdwId = allocate.getBbdw_id();
				Long fpdx = allocate.getFpdx();
				//是否是本省。需要检测他有没有来源信息
				if(allocate.getFpfs().equals(IpAllocateType.RE_ALLOCATE.getTypeValue()) && bsdwMap.get(fpdx) == null){
					 for(IpBean source : reduceSourceList){
						 if(!new Boolean(false).equals(source.getIsPass())){
							 if(bbdwId.equals(source.getBbdw_id())){
								 allocate.setIsPass(true);
								 flag = true;
								 break;
							 }else{
								 flag = false;
							 }
						 }else{
							 continue;
						 }
					 }
					 if(!flag){
//						 allocate.setIsPass(false);
//						 redisDao.hset(IpCheckConstants.CONFLICT_DATA_KEY, key.toString(), "");
						 ErrIp errIp = new ErrIp();
						 errIp.setBbdws(allocate.getBbdw_id().toString());
						 errIp.setRegionIds(allocate.getStorageType() + "_" + allocate.getTflag() + "_" + allocate.getId());
						 errIp.setJsonStr(allocate.getConflictMsg());
						 if(allocate.getStorageType().equals(IpCheckConstants.BEIAN)){
							 errIp.setConflictType(ConflictType.IP_ALO_OVERSTEP_SOURCE_OMIT_ERROR.getTypeValue());
							 errIp.setStorageType(IpCheckConstants.BEIAN);
							}else if(allocate.getStorageType().equals(IpCheckConstants.UPLOAD)){
								errIp.setConflictType(ConflictType.IP_ALO_OVERSTEP_SOURCE_OMIT_ERROR2.getTypeValue());
								errIp.setStorageType(IpCheckConstants.UPLOAD);
							}
						 objectInfo.setErrInfo(errIp);
						 objectInfo.setResult(false);
						 return objectInfo;
					 }
				}else{
					//那么检查A分配给B在来源库有B来源于A的记录，需要区分分配单位是否是本省外省
					if(allocate.getFpfs().equals(IpAllocateType.RE_ALLOCATE.getTypeValue())){
						 //检查来源库有没有这个IP
						 if(reduceSourceList == null || reduceSourceList.size() == 0){
							 //来源库没有这个IP，那么就属于分配越界或来源漏报
//							 	log.info("来源库没有这个IP" + key.toString() + "|" + allocate.toString());
//							 redisDao.hset(IpCheckConstants.CONFLICT_DATA_KEY, key.toString(), "");
//							 log.info("scanner89冲突ip:"+newIp);
//								 allocate.setIsPass(false);
								 ErrIp errIp = new ErrIp();
								 errIp.setRegionIds(allocate.getStorageType() + "_" + allocate.getTflag() + "_" + allocate.getId());
								 errIp.setJsonStr(allocate.getConflictMsg());
								 if(allocate.getStorageType().equals(IpCheckConstants.BEIAN)){
//									 if(bbdwId > fpdx){
										 errIp.setBbdws(bbdwId + "," + fpdx);
//									 }else{
//										 errIp.setBbdws(fpdx + "," + bbdwId);
//									 }
									 errIp.setConflictType(ConflictType.IP_ALO_OVERSTEP_SOURCE_OMIT_ERROR.getTypeValue());
									 errIp.setStorageType(IpCheckConstants.BEIAN);
								 }else if(allocate.getStorageType().equals(IpCheckConstants.UPLOAD)){
//									 if(bbdwId > fpdx){
										 errIp.setBbdws(bbdwId + "," + fpdx);
//									 }else{
//										 errIp.setBbdws(fpdx + "," + bbdwId);
//									 }
									 errIp.setConflictType(ConflictType.IP_ALO_OVERSTEP_SOURCE_OMIT_ERROR2.getTypeValue());
									 errIp.setStorageType(IpCheckConstants.UPLOAD);
								 }
								 objectInfo.setErrInfo(errIp);
								 objectInfo.setResult(false);
								 return objectInfo;
						 }else{
							 //来源库有这个IP
							 List<IpBean> sourceList = reduceSourceList;
							 for(IpBean source : sourceList){
								 if(!new Boolean(false).equals(source.getIsPass())){
									 if(bbdwId.equals(source.getLyid()) && fpdx.equals(source.getBbdw_id())){   //bbdwId.equals(source.getBbdw_id() ???
										 flag = true;
										 break;
									 }else{
										 continue;
									 }
								 }else{
									 continue;
								 }
							 }
							 if(flag == false){
								 //分配库有，但是来源库没有，那么就是分配越界或来源漏报
//								 redisDao.hset(IpCheckConstants.CONFLICT_DATA_KEY, key.toString(), "");
//								 log.info("scanner130冲突ip:"+newIp);
//								 allocate.setIsPass(false);
								 ErrIp errIp = new ErrIp();
								 errIp.setRegionIds(allocate.getStorageType() + "_" + allocate.getTflag() + "_" + allocate.getId());
								 errIp.setJsonStr(allocate.getConflictMsg());
								 if(allocate.getStorageType().equals(IpCheckConstants.BEIAN)){
//									 if(bbdwId > fpdx){
										 errIp.setBbdws(bbdwId + "," + fpdx);
//									 }else{
//										 errIp.setBbdws(fpdx + "," + bbdwId);
//									 }
									 errIp.setConflictType(ConflictType.IP_ALO_OVERSTEP_SOURCE_OMIT_ERROR.getTypeValue());
									 errIp.setStorageType(IpCheckConstants.BEIAN);
								 }else if(allocate.getStorageType().equals(IpCheckConstants.UPLOAD)){
//									 if(bbdwId > fpdx){
										 errIp.setBbdws(bbdwId + "," + fpdx);
//									 }else{
//										 errIp.setBbdws(fpdx + "," + bbdwId);
//									 }
									 errIp.setConflictType(ConflictType.IP_ALO_OVERSTEP_SOURCE_OMIT_ERROR2.getTypeValue());
									 errIp.setStorageType(IpCheckConstants.UPLOAD);
								 }
								 objectInfo.setErrInfo(errIp);
								 objectInfo.setResult(false);
								 return objectInfo;
							 }
						 }// end if-else
					}else{
						//自带不考虑，需要在来源库找到对应来源信息
						if(!allocate.getFpfs().equals(IpAllocateType.HOLDING_BY_SELF.getTypeValue())){
							//有没有这个IP 属于分配越界或来源漏报
//							log.info("---来源库没有这个IP");
//							log.info("scanner161冲突ip:"+newIp);
							 if(reduceSourceList == null || reduceSourceList.size() == 0){
//								 redisDao.hset(IpCheckConstants.CONFLICT_DATA_KEY, key.toString(), "");
								 ErrIp errIp = new ErrIp();
								 errIp.setBbdws(allocate.getBbdw_id().toString());
								 errIp.setRegionIds(allocate.getStorageType() + "_" + allocate.getTflag() + "_" + allocate.getId());
								 errIp.setJsonStr(allocate.getConflictMsg());
								 if(allocate.getStorageType().equals(IpCheckConstants.BEIAN)){
										errIp.setConflictType(ConflictType.IP_ALO_OVERSTEP_SOURCE_OMIT_ERROR.getTypeValue());
										errIp.setStorageType(IpCheckConstants.BEIAN);
									}else if(allocate.getStorageType().equals(IpCheckConstants.UPLOAD)){
										errIp.setConflictType(ConflictType.IP_ALO_OVERSTEP_SOURCE_OMIT_ERROR2.getTypeValue());
										errIp.setStorageType(IpCheckConstants.UPLOAD);
									}
								 objectInfo.setErrInfo(errIp);
								 objectInfo.setResult(false);
								 return objectInfo;
							 }else{
								 for(IpBean source : reduceSourceList){
									 if(!new Boolean(false).equals(source.getIsPass())){
										 if(bbdwId.equals(source.getBbdw_id())){
											 allocate.setIsPass(true);
											 flag = true;
											 break;
										 }else{
											 flag = false;
										 }
									 }else{
										 continue;
									 }
								 }
								 if(!flag){
//									 allocate.setIsPass(false);
//									 redisDao.hset(IpCheckConstants.CONFLICT_DATA_KEY, key.toString(), "");
									 ErrIp errIp = new ErrIp();
									 errIp.setBbdws(allocate.getBbdw_id().toString());
									 errIp.setRegionIds(allocate.getStorageType() + "_" + allocate.getTflag() + "_" + allocate.getId());
									 errIp.setJsonStr(allocate.getConflictMsg());
									 if(allocate.getStorageType().equals(IpCheckConstants.BEIAN)){
											errIp.setConflictType(ConflictType.IP_ALO_OVERSTEP_SOURCE_OMIT_ERROR.getTypeValue());
											errIp.setStorageType(IpCheckConstants.BEIAN);
										}else if(allocate.getStorageType().equals(IpCheckConstants.UPLOAD)){
											errIp.setConflictType(ConflictType.IP_ALO_OVERSTEP_SOURCE_OMIT_ERROR2.getTypeValue());
											errIp.setStorageType(IpCheckConstants.UPLOAD);
										}
									 objectInfo.setErrInfo(errIp);
									 objectInfo.setResult(false);
									 return objectInfo;
								 }
							 }
						}
					}
				}
		}else{
			continue;
		}
	}
		return objectInfo;
	}
	
	
}
